class Solution {
public:
    int dfs(TreeNode* root, int sum)
    {
        sum = sum * 10 + root->val;
        if (root->left == nullptr && root->right == nullptr) return sum;

        int ret = 0;
        if (root->left) ret += dfs(root->left, sum);
        if (root->right) ret += dfs(root->right, sum);
        return ret;

    }
    int sumNumbers(TreeNode* root) {
        return dfs(root, 0);
    }
};